-- Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskType_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskType_Insert]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskType_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskType_Update]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskType_InsertUpdate]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskType_InsertUpdate]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskType_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskType_Delete]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskType_DeleteDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskType_DeleteDynamic]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskType_Load]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskType_Load]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskType_SelectDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskType_SelectDynamic]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskType_SelectAll]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskType_SelectAll]


GO


------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskType_Insert]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskType_Insert]
	@ModuleID uniqueidentifier,
	@Title nvarchar(128),
	@ID int OUTPUT
AS

INSERT INTO [dbo].[t_ACTP_TaskTypes]
(
	[ModuleID],
	[Title]
)
VALUES 
(
	@ModuleID,
	@Title
)

SET @ID = SCOPE_IDENTITY()

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskType_Update]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskType_Update]
	@ID int,
	@ModuleID uniqueidentifier,
	@Title nvarchar(128)
AS

UPDATE
	[dbo].[t_ACTP_TaskTypes]
SET
	[ModuleID] = @ModuleID,
	[Title] = @Title
WHERE
	[ID] = @ID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskType_InsertUpdate]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskType_InsertUpdate]
	@ID int,
	@ModuleID uniqueidentifier,
	@Title nvarchar(128)
AS
IF EXISTS(SELECT [ID] FROM [dbo].[t_ACTP_TaskTypes] WHERE [ID] = @ID)
	BEGIN
		UPDATE
			[dbo].[t_ACTP_TaskTypes] 
		SET
			[ModuleID] = @ModuleID,
			[Title] = @Title
		WHERE
			[ID] = @ID
	END
ELSE
	BEGIN
		
		INSERT INTO [dbo].[t_ACTP_TaskTypes]
		(
			[ModuleID],
			[Title]
		)
		VALUES 
		(
			@ModuleID,
			@Title
		)		
	END
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskType_Delete]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskType_Delete]
	@ID int
AS

DELETE FROM 
	[dbo].[t_ACTP_TaskTypes]
WHERE
	[ID] = @ID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskType_DeleteDynamic]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskType_DeleteDynamic]
	@WhereCondition NVARCHAR(500)
AS

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 'DELETE FROM [dbo].[t_ACTP_TaskTypes] WHERE ' + @WhereCondition

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskType_Load]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskType_Load]
	@ID int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[ModuleID],
	[Title]
FROM
	[dbo].[t_ACTP_TaskTypes]
WHERE
	[ID] = @ID
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskType_SelectDynamic]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskType_SelectDynamic]
	@WhereCondition NVARCHAR(500),
	@OrderByExpression NVARCHAR(250) = NULL
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 
'SELECT 
	[ID],
	[ModuleID],
	[Title]
FROM [dbo].[t_ACTP_TaskTypes] 
WHERE ' + @WhereCondition

IF @OrderByExpression IS NOT NULL AND LEN(@OrderByExpression) > 0
BEGIN
	SET @SQL = @SQL + ' ORDER BY ' + @OrderByExpression
END

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskType_SelectAll]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskType_SelectAll]
	@ModuleID UNIQUEIDENTIFIER
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[ModuleID],
	[Title]
FROM
	[dbo].[t_ACTP_TaskTypes]	
WHERE 
	(ModuleID = @ModuleID)

GO

